package com.cn.onetrip.activity;

import java.util.List;

import com.cn.onetrip.objects.AtlasObj.PictureObj;
import com.cn.onetrip.rorate3d.GalleryFlow;
import com.cn.onetrip.rorate3d.ImageAdapter;
import com.cn.onetrip.activity.R;
import com.cn.onetrip.untility.SysApplication;
import com.cn.onetrip.untility.Untility;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class GalleryFlowActivity extends Activity {

	public static View view;
	private Button quit;
	private String texts[], imageUrl[];
	private int num = 0;
	private GalleryFlow galleryFlow;
	private ImageAdapter adapter;
	private int count = 0;
	private ProgressBar pb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gallery);
		Window w = getWindow();
		view = w.getDecorView();

		List<PictureObj> slidesList = SysApplication.slidesList;
		texts = new String[slidesList.size()];
		imageUrl = new String[slidesList.size()];
		for (int i = 0; i < slidesList.size(); i++) {
			PictureObj pictureObj = slidesList.get(i);
			texts[i] = pictureObj.imageDescription;
			imageUrl[i] = pictureObj.imageUrl;
		}

		adapter = new ImageAdapter(GalleryFlowActivity.this, mHandler,
				imageUrl, texts);
		galleryFlow = (GalleryFlow) findViewById(R.id.Gallery01);
		galleryFlow.setVerticalFadingEdgeEnabled(false);
		galleryFlow.setHorizontalFadingEdgeEnabled(false);
		// galleryFlow.setAdapter(adapter);
		pb = (ProgressBar) findViewById(R.id.GalleryPb);
		quit = (Button) findViewById(R.id.quit);
		quit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// view.startAnimation(Untility.rQuest1Animation);
				// GroupActivity.changeFirstView();
				finish();
				overridePendingTransition(R.anim.push_left_translate,
						R.anim.pull_right_translate);
			}
		});
	}

	@Override
	protected void onResume() {
		GalleryWhetherStop();
		super.onResume();
	}

	@Override
	protected void onPause() {
		adapter.threadQuit();
		super.onPause();
	}

	private void GalleryWhetherStop() {
		Runnable runnable = new Runnable() {
			public void run() {
				Message m = new Message();
				m.what = 1;
				mHandler.sendMessage(m);
			}
		};
		new Thread(runnable).start();
	}

	class LoadImageTask extends AsyncTask<String, Void, Bitmap> {
		@Override
		protected void onCancelled() {
			super.onCancelled();
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			super.onPostExecute(result);
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			Bitmap bitmap = null;
			try {
				String url = params[0];
				ImageAdapter.loadImageFromUrl(url);
				count++;
				if (count == imageUrl.length) {
					count = 0;
					Thread.sleep(500);
					Message m = new Message();
					m.what = 0;
					mHandler.sendMessage(m);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return bitmap;
		}
	}

	public void setInvisibleText() {
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			overridePendingTransition(R.anim.push_left_translate,
					R.anim.pull_right_translate);
		}
		return super.onKeyDown(keyCode, event);
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			try {
				switch (msg.what) {
				case 0: {
					galleryFlow.setAdapter(adapter);
					adapter.notifyDataSetChanged();
					pb.setVisibility(View.GONE);
					break;
				}
				case 1: {
					for (int i = 0; i < imageUrl.length; i++) {
						LoadImageTask task = new LoadImageTask();
						task.execute(imageUrl[i]);
					}
				}
				}
				super.handleMessage(msg);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};

}
